//
// Created by 周智超 on 2020/6/15.
//

#include "string.h"
#include <iostream>
#include "gtest/gtest.h"

TEST(Next, t1)
{
    int next[maxSize];
    int nextVal[maxSize];
    char substr[] = "0abcabc";
    int flag;
    nextV1(substr, next);
    std::cout << "nextVal" << std::endl;
    nextValV1(substr, nextVal);
    flag = kmp("0dababcabcefg", substr, next);
    ASSERT_TRUE(flag > 0);
    flag = kmp("0dababcabcefg", substr, nextVal);
    ASSERT_TRUE(flag > 0);

    char po[] = "0aacdefcdefaaacdeffcdecde";
    char ps[] = "0cde";
    int pnext[maxSize];
    nextValV1(ps, pnext);
    flag =  kmpPos(po, ps, pnext, 1);
    ASSERT_EQ(flag, 3);
    flag =  kmpPos(po, ps, pnext, 2);
    ASSERT_EQ(flag, 3);
    flag =  kmpPos(po, ps, pnext, 3);
    ASSERT_EQ(flag, 3);
    flag =  kmpPos(po, ps, pnext, 4);
    ASSERT_EQ(flag, 7);
    flag = showTimes(po, ps, pnext);
    ASSERT_EQ(flag, 5);
}

TEST(exercise, del1)
{
    Str *s1;
    initStr(s1, "hello world");
    std::cout << "pre:" << s1->length << " " <<  s1->data << std::endl;
    del(s1, 5, 7);
    std::cout << "a1:" << s1->length << " " <<  s1->data << std::endl;
    Str *s2;
    initStr(s2, "hello world");
    del(s2, 5, 20);
    std::cout << "a2:" << s2->length << " " <<  s2->data << std::endl;

}

TEST(exercise, insert)
{
    Str *s1;
    initStr(s1, "say hello to world");
    int ol = s1->length;

    Str *s2;
    initStr(s2, "shit");
    insert(s1, s2, 4, 8);
    std::cout << "b1:" << s1->length << " " <<  s1->data << std::endl;
    ASSERT_EQ(ol-1, s1->length);
    {
        Str *s1;
        initStr(s1, "say hello to world");
        int ol = s1->length;
        std::cout << "a2:" << s1->length << " " << s1->data << std::endl;

        Str *s2;
        initStr(s2, "best luck");
        insert(s1, s2, 4, 8);
        std::cout << "b1:" << s1->length << " " << s1->data << std::endl;
        ASSERT_EQ(ol + 4, s1->length);
        ASSERT_STREQ("say best luck to world", s1->data);
    }


}

TEST(Next, t2)
{
    int next[maxSize];
    int nextVal[maxSize];
    char substr[] = "0ababaaababaa";
    std::cout << "next " << std::endl;
    nextV1(substr, next);
    std::cout << "nextVal " << std::endl;
    nextValV1(substr, nextVal);
}

TEST(exercise, replace)
{
    char a1[] = "hello world";
    replace(a1, 'l', 'm');
    ASSERT_STREQ(a1, "hemmo wormd");
    std::cout << a1 << std::endl;
    char a2[] = "";
    replace(a2, 'l', 'm');
    ASSERT_STREQ(a2, "");
}

TEST(exercise, reverse)
{
    char a1[] = "hello";
    reverse(a1);
    ASSERT_STREQ(a1, "olleh");
    char a2[] = "";
    reverse(a2);
    ASSERT_STREQ(a2, "");

    char a3[] = "abc ";
    reverse(a3);
    ASSERT_STREQ(a3, " cba");

}


TEST(exercise, removeElement)
{
    char a1[] = "hello";
    removeElement(a1, 'l');
    ASSERT_STREQ(a1, "heo");

    char a2[] = "oooo";
    removeElement(a2, 'o');
    ASSERT_STREQ(a2, "");

    char a3[] = "";
    removeElement(a3, 'o');
    ASSERT_STREQ(a3, "");

}

int main(int argc,char *argv[])
{
    testing::InitGoogleTest(&argc, argv);//将命令行参数传递给gtest
    return RUN_ALL_TESTS();   //RUN_ALL_TESTS()运行所有测试案例
}